<?php
/**
 * 全局自定义辅助函数
 * Created by PhpStorm.
 * User: jinqiang
 * Date: 2018/12/7
 * Time: 20:53
 */

use Illuminate\Support\Facades\Redis;

/**
 * 获取指定长度的随机码
 */
if (!function_exists('randCode')) {
    function randCode($long = 4, $code = '0123456789abcdefghijkmnpqrstuvwxyz')
    {
        $res = '';
        $size = strlen($code);
        for ($i = 0; $i < $long; $i++) {
            $res .= $code[rand(0, $size - 1)];
        }
        return $res;
    }
}

/**
 * 返回数据
 */
if (!function_exists('result')) {
    function result($data = [], $msg = '获取数据')
    {
        $response = [
            'code' => 0,
            'msg' => $msg,
            'data' => $data
        ];
        return response()->json($response);
    }
}

/**
 * 返回成功
 */
if (!function_exists('success')) {
    function success($msg = '操作成功', $data = [])
    {
        $response = [
            'code' => 0,
            'msg' => $msg,
            'data' => $data
        ];
        return response()->json($response);
    }
}

/**
 * 返回失败
 */
if (!function_exists('error')) {
    function error($msg = '操作失败', $data = [], $code = -1)
    {
        $response = [
            'code' => $code,
            'msg' => $msg,
            'data' => $data
        ];
        return response()->json($response);
    }
}

/**
 * 生成唯一字符串
 */
if (!function_exists('getID')) {
    function getID($str)
    {
        $m = md5($str);
        $res = "";
        for ($i = 0; $i < 24; $i++) {
            $res .= dechx(hexdec($m[$i]) ^ hexdec($m[$i + 8]));
        }
        return $res;
    }
}

/**
 *请求接口curl
 */
if (!function_exists('client')) {
    function client($url, $form = [], $type = 'get')
    {
        try {
            $client = new \GuzzleHttp\Client();
            $res = $client->request($type, $url, $form);
            if ($res->getStatusCode() != 200) {
                return ['code' => -1, 'msg' => $res];
            }
            if (isset($form['form_params'])) {
                return $res->getBody()->getContents();
            }
            $body = json_decode($res->getBody(), true);
            return $body;
        } catch (Exception $e) {
            $code = 500;
            $msg = "500 Error: 接口请求错误";
            if (preg_match('/timed out/is', $e->getMessage())) {
                $code = 504;
                $msg = "504 Error: 接口请求超时";
            }
            return ['code' => $code, 'msg' => $msg, 'data' => $e->getMessage()];
        }
    }
}

/**
 * 爬取网页内容
 */
if (!function_exists('ppopen')) {
    function ppopen($cmd)
    {
        $res = '';
        $ft = popen($cmd, 'r');
        while (!feof($ft)) {
            $res .= fgets($ft, 2048);
        }
        pclose($ft);
        return $res;
    }
}

/**
 * 合成图片的标题处理
 */
if (!function_exists('splitContent')) {
    function splitContent($string)
    {
        $string = preg_replace('/[\n\r]/is', '', $string);
        $p = 78;
        $zhLen = mb_strlen($string);
        $str = "";
        $len = 0;
        for ($i = 0; $i < $zhLen; $i++) {
            $s = mb_substr($string, $i, 1);
            $len += strlen($s) < 3 ? 2 : 3;
            if ($len > $p) {
                $len = 0;
                $str .= "\n" . $s;
            } else {
                $str .= $s;
            }
        }
        return $str;
    }
}

/**
 * 去除 空行内容
 */
if (!function_exists('removeEmptyLine')) {
    function removeEmptyLine($string, $split = "\r")
    {
        $arr = explode($split, $string);
        $content = "";
        foreach ($arr as $value) {
            $value = trim($value);
            !empty($value) && $content .= (empty($content) ? "" : $split) . $value;
        }
        return $content;
    }
}

/**
 * 获取服务器host地址
 */
if (!function_exists('getServerHost')) {
    function getServerHost($serverKey, $id)
    {
        return Redis::hget($serverKey, $id);
    }
}

/**
 * 关键词替换
 */
if (!function_exists('replaceWord')) {
    function replaceWord($replaceWord, $str)
    {
        try {
            if (is_array($replaceWord) && isset($replaceWord[0]) && isset($replaceWord[1])
                && !empty($replaceWord[0]) && !empty($replaceWord[1])) {
                return str_replace($replaceWord[0], $replaceWord[1], $str);
            }
            return $str;
        } catch (Exception $e) {
            return $str;
        }
    }
}

/**
 * 是否匹配到关键词
 */
if (!function_exists('matchWord')) {
    function matchWord($filterWord, $str)
    {
        try {
            if (empty($filterWord)) {
                return false;
            }
            if (is_array($filterWord)) {
                foreach ($filterWord as $val) {
                    if (strstr($str, $val)) {
                        return true;
                    }
                }
            } else {
                return strstr($str, $filterWord);
            }
        } catch (Exception $e) {
            return false;
        }
        return false;
    }
}

/**
 * 淘口令 模拟创建
 */
if (!function_exists('createTokenMode')) {
    function createTokenMode($token)
    {
        $limit = random_int(8,15);
        $arr = str_split($token);
        $data = ["$".$token."$"];
        for ($i = 1; $i <= $limit; $i++){
            shuffle($arr);
            $data[] = "$".implode("", $arr)."$";
        }
        shuffle($data);
        return implode("", $data);
    }
}

